package com.tys.algorithm.beginner;

import java.util.Comparator;
import java.util.PriorityQueue;

public class Code20_ShowComparator {

    public static class Student {
        public String name;
        public int id;
        public int age;

        public Student(String name, int id, int age) {
            this.name = name;
            this.id = id;
            this.age = age;
        }
    }

    public static class MyComparator implements Comparator<Integer> {

        @Override
        public int compare(Integer o1, Integer o2) {
            if (o1 < o2) {
                return 1;
            } else if (o1 > o2) {
                return -1;
            } else {
                return 0;
            }
        }
    }

    //Id比较器
    public static class IdComparator implements Comparator<Student> {
        @Override
        public int compare(Student o1, Student o2) {
            if (o1.id < o2.id) { //大的在前面排
                return 1; //返回正，则第二个参数在前面排
            } else if (o2.id < o1.id) {
                return -1;//返回负，第一个参数在前面排
            } else {
                return 0;//返回0，哪个排前面无所谓
            }
        }
    }


    public static void main(String[] args) {
        PriorityQueue<Integer> heap = new PriorityQueue<>(new MyComparator());
        heap.add(6);
        heap.add(2);
        heap.add(3);
        heap.add(1);
        heap.add(7);
        System.out.println(heap.peek());
        heap.add(0);
        System.out.println(heap.peek());
        System.out.println("==========");
        while (!heap.isEmpty()) {
            System.out.println(heap.poll());
        }

        PriorityQueue<Student> heap2 = new PriorityQueue<Student>(new IdComparator());
        //自定义类排序
        Student s1 = new Student("张三", 5, 27);
        Student s2 = new Student("李四", 1, 17);
        Student s3 = new Student("王五", 4, 29);
        Student s4 = new Student("赵六", 3, 9);
        Student s5 = new Student("左七", 2, 34);
        heap2.add(s1);
        heap2.add(s2);
        heap2.add(s3);
        heap2.add(s4);
        heap2.add(s5);
        System.out.println("==========");
        while (!heap2.isEmpty()) {
            Student s = heap2.poll();
            System.out.println(s.name + ", " + s.id + ", " + s.age);
        }


        System.out.println("==========");

        String str1 = "abc";
        String str2 = "b";
        System.out.println(str1.compareTo(str2));


    }
}
